﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.Properties;
using SPVSX.Explorer.Extensions.NodeInformation;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;
using System.Diagnostics;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Extensions
{
    /// <summary>
    /// Defines a new node type that represents a theme on a SharePoint site.
    /// </summary>
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeProvider))]
    // Specifies the ID for this new node type.
    [ExplorerNodeType(ThemeNodeTypeProvider.ThemeNodeTypeId)]
    class ThemeNodeTypeProvider : IExplorerNodeTypeProvider
    {
        #region Constants

        /// <summary>
        /// The node type id
        /// </summary>
        internal const string ThemeNodeTypeId = "SPVSX.SharePoint.Theme";

        #endregion

        #region Methods

        /// <summary>
        /// Initialise the node with its icon and event hookups
        /// </summary>
        /// <param name="typeDefinition">The node type defintion</param>
        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon = Resources.ThemeNode.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;

            typeDefinition.NodePropertiesRequested += NodePropertiesRequested;
            typeDefinition.NodeMenuItemsRequested += NodeMenuItemsRequested;
        }

        private void NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            e.MenuItems.Add(Resources.ThemeNode_Export, 4).Click += ThemeNodeTypeProvider_Click;
        }

        void ThemeNodeTypeProvider_Click(object sender, MenuItemEventArgs e)
        {
            IMenuItem menuItem = sender as IMenuItem;

            IExplorerNode owner = (IExplorerNode)e.Owner;

            ThemeNodeInfo info = owner.GetAnnotation<ThemeNodeInfo>();

            if (menuItem.Text == Resources.ThemeNode_Export)
            {
                ThemeMenuItemClick_Export(owner, info);
            }
        }
        
        private static void ThemeMenuItemClick_Export(IExplorerNode owner, ThemeNodeInfo info)
        {
            if (info != null)
            {
                Process.Start(new Uri(owner.Context.SiteUrl + info.Url.TrimStart(@"/".ToCharArray())).AbsoluteUri);
            }
        }

        /// <summary>
        /// Retrieves properties that are displayed in the Properties window when
        /// a theme node is selected.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodePropertiesRequestedEventArgs object</param>
        private void NodePropertiesRequested(object sender,
            ExplorerNodePropertiesRequestedEventArgs e)
        {
            var themeNodeInfo = e.Node.GetAnnotation<ThemeNodeInfo>();

            // Call the custom SharePoint command to get the theme properties.
            Dictionary<string, string> properties =
                e.Node.Context.SharePointConnection.ExecuteCommand<
                ThemeNodeInfo, Dictionary<string, string>>(
                ThemeCommandIds.GetThemeProperties, themeNodeInfo);

            object propertySource = e.Node.Context.CreatePropertySourceObject(properties);
            e.PropertySources.Add(propertySource);
        }

        #endregion
    }
}